// JavaScript Document 
refreshMenu = "";
cutID = "";
copyID = "";
pasteID = "";

//###################################
//RIGHT CLICK CONTEXT MENU FUNCTIONS 
//###################################
function setMenuOptions(isCategory){
	if(isCategory == 1){//setup right click menu for category options
		dijit.byId('menuView').setDisabled(true);
		dijit.byId('menuDelete').setDisabled(false);
		dijit.byId('menuRename').setDisabled(false);
		dijit.byId('menuCut').setDisabled(true);
		dijit.byId('menuCopy').setDisabled(true);
		if(cutID != "" || copyID != ""){
			dijit.byId('menuPaste').setDisabled(false);
		}else{
			dijit.byId('menuPaste').setDisabled(true);
		}
	}else if(isCategory == 0){//setup right click menu for content options
		dijit.byId('menuView').setDisabled(false);
		dijit.byId('menuDelete').setDisabled(false);
		dijit.byId('menuRename').setDisabled(false);
		dijit.byId('menuCut').setDisabled(false);
		dijit.byId('menuCopy').setDisabled(false);
		dijit.byId('menuPaste').setDisabled(true);
	}else if(isCategory == 5){//disable all b/c it is probably the root node
		dijit.byId('menuView').setDisabled(true);
		dijit.byId('menuDelete').setDisabled(true);
		dijit.byId('menuRename').setDisabled(true);
		dijit.byId('menuCut').setDisabled(true);
		dijit.byId('menuCopy').setDisabled(true);
		dijit.byId('menuPaste').setDisabled(true);
	}
}

function view(){
	window.open(XOOPS_URL_NON_SSL+"/modules/content/index.php?id="+contextStoryId,"preview","")
}


//#######################################
//RENAME MENU RIGHT CLICK MENU ITEM
//#######################################
function rename(){
	dijit.byId('renameDialog').show();
	dojo.byId('renameTitleHolder').innerHTML = contextItemTitle;
	//dojo.byId('renameID').innerHTML = contextStoryId;
	//dojo.byId('contOrCategory').innerHTML = contextIsCategory;
	//dojo.byId('categoryTitle').innerHTML = contextItemTitle;
}

function submitRenameMenuItem(){
	if(dijit.byId('renameMenuItem').getDisplayedValue() == ""){
		alert('Please enter a new name for \"'+contextItemTitle+'\"');
		dijit.byId('renameDialog').show();
	}else{
		dojo.xhrPost({
		  url: "fetchData.php?action=renameMenuItem",
		  handleAs: "json-comment-filtered",
		  content: {renameID:contextStoryId,contOrCategory:contOrCategory,oldTitle:contextItemTitle,renameMenuItem:dijit.byId('renameMenuItem').getDisplayedValue()},
		  timeout: 5000,
		  handle: function(response, ioArgs){
		  	dijit.byId('renameMenuItem').setDisplayedValue('');
		  	dijit.byId('renameDialog').hide();
		  	dijit.byId('systemMessages').setContent("You have successfully renamed "+contextItemTitle+ " as: \""+response.category+".\" ");
		  	dijit.byId('systemMessages').toggle();	  	
		  	setTimeout('dijit.byId(\'systemMessages\').toggle();dijit.byId(\'dropDownContentPane\').refresh();dijit.byId(\'menuContentPane\').refresh();', 4000);
		  }
		});
	}
	
}

//#######################################
//DELETE MENU RIGHT CLICK MENU ITEM
//#######################################
function deleteItem(){
	if(contOrCategory == 'content'){
		dijit.byId('deletDialog').show();
		dojo.byId('deleteContentTitleHolder').innerHTML = contextItemTitle;
	}else if(contOrCategory == 'category'){
		dijit.byId('deleteCategoryDialog').show();
		dojo.byId('deleteCategoryTitleHolder').innerHTML = contextItemTitle;
	}
}

function submitDeleteMenuItem(){

		dojo.xhrPost({
		  url: "fetchData.php?action=deleteMenuItem",
		  handleAs: "json-comment-filtered",
		  content: {deleteID:contextStoryId,contOrCategory:contOrCategory,oldTitle:contextItemTitle},
		  timeout: 5000,
		  handle: function(response, ioArgs){
		  
		  	if(contOrCategory == 'content'){
		  		dijit.byId('deletDialog').hide();
		  	}else{
		  		dijit.byId('deleteCategoryDialog').hide();
		  	} 
		  	
		  	dijit.byId('systemMessages').setContent("You have successfully deleted "+contextItemTitle);
		  	dijit.byId('systemMessages').toggle();	  	
		  	setTimeout('dijit.byId(\'systemMessages\').toggle();dijit.byId(\'dropDownContentPane\').refresh();dijit.byId(\'menuContentPane\').refresh();', 4000);
		  }
		});

	
}

//#######################################
//CUT, COPY, AND PASTE MENU ITEMS
//#######################################

function cut(){
	cutID = contextStoryId;
}

function copy(){
	copyID = contextStoryId;
}

function paste(){
	if(cutID != ""){
	
		dojo.xhrPost({
		  url: "fetchData.php?action=cut",
		  handleAs: "json-comment-filtered",
		  content: {storyid:cutID,toCategory:contextItemTitle},
		  timeout: 5000,
		  handle: function(response, ioArgs){ 	
		  	setTimeout('dijit.byId(\'menuContentPane\').refresh();', 500);
		  }
		});

	}else if(copyID != ""){
		dojo.xhrPost({
		  url: "fetchData.php?action=paste",
		  handleAs: "json-comment-filtered",
		  content: {storyid:copyID,toCategory:contextItemTitle},
		  timeout: 5000,
		  handle: function(response, ioArgs){ 	
		  	setTimeout('dijit.byId(\'menuContentPane\').refresh();', 500);
		  }
		});
	}
	
	//reset cut and copy variables
	cutID = "";
	copyID = "";
}

//######################################
//END RIGHT CLICK CONTEXT MENU FUNCTIONS 
//######################################






//###################################
//EXECUTE FCKEditor COMMANDS
//###################################
function ExecuteCommand( commandName )
{
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance('body') ;

	// Execute the command.
	oEditor.Commands.GetCommand( commandName ).Execute() ;
}

function clearContentForm(){
	ExecuteCommand('NewPage');
	dijit.byId('title').setDisplayedValue('');
	dojo.byId('title').focus();
	dijit.byId('editID').setDisplayedValue('');
}


//####################################
//SELECT CONTENT FOR EDITING DB
//####################################
function select_content(storyId,XOOPS_URL){

//get storyid, title, and category
		dojo.xhrGet({
		        url: XOOPS_URL+"/modules/content/admin/fetchData.php?storyId="+storyId+"&action=select_content&body=false",
		        handleAs: "json-comment-filtered",
		        timeout: 5000, //Time in milliseconds
		        handle: function(response, ioArgs){
		                //This function handles the response.
		                //Inside this function, the "this" variable
		                //will be the object used as the argument to the dojo.xhrGet() call.
		                if(response instanceof Error){
		                        if(response.dojoType == "cancel"){
		                                //The request was canceled by some other JavaScript code.
		                                console.debug("Request canceled.");
		                                var messages = "Request canceled.";
		                        }else if(response.dojoType == "timeout"){
		                                //The request took over 5 seconds to complete.
		                                console.debug("Request timed out.");
		                                var messages = "Request timed out.";
		                        }else{
		                                //Some other error happened.
		                                console.error(response);
		                                var messages = "An error occured, please view the console";
		                                
		                        }
		                        
		                        module_select_handler(response,messages,'false');
		                }else{//successful query
		                			                	
		                	module_select_handler(response,'','false');
		                	
		                
		                
		                }//
		                
		                
						
		        }
		});
		


//-----------------------------------------------------------		
//get body as a text blob
//-----------------------------------------------------------		
				dojo.xhrGet({
		        url: XOOPS_URL+"/modules/content/admin/fetchData.php?storyId="+storyId+"&action=select_content&body=true",
		        handleAs: "text",
		        timeout: 5000, //Time in milliseconds
		        handle: function(response, ioArgs){
		                //This function handles the response.
		                //Inside this function, the "this" variable
		                //will be the object used as the argument to the dojo.xhrGet() call.
		                if(response instanceof Error){
		                        if(response.dojoType == "cancel"){
		                                //The request was canceled by some other JavaScript code.
		                                console.debug("Request canceled.");
		                                var messages = "Request canceled.";
		                        }else if(response.dojoType == "timeout"){
		                                //The request took over 5 seconds to complete.
		                                console.debug("Request timed out.");
		                                var messages = "Request timed out.";
		                        }else{
		                                //Some other error happened.
		                                console.error(response);
		                                var messages = "An error occured, please view the console";
		                                
		                        }
		                        
		                        module_select_handler(response,messages,'true');
		                }else{//successful query
		                			                	
		                	module_select_handler(response,'','true');
		                	
		                
		                
		                }//
		                
		                
						
		        }
		});
		
		
		
		
		

//last
}


//#######################################
//SUBMIT CONTENT FORM TO DB
//#######################################
function submitContentForm(){

//set hidden field to value of FCKEditor to submit
var oEditor = FCKeditorAPI.GetInstance('body') ;
dijit.byId('FCKBody').setDisplayedValue(oEditor.GetXHTML());


		//form validation
		if(dijit.byId('title').getValue() == ""){
			dijit.byId('systemMessages').setContent("Please enter a title before submitting.");
			dijit.byId('systemMessages').toggle();
			dojo.byId('title').focus();
			setTimeout('dijit.byId(\'systemMessages\').toggle();', 4000);
		}else if(dijit.byId('FCKBody').getValue() == ""){
			dijit.byId('systemMessages').setContent("Please enter content before submitting.");
			dijit.byId('systemMessages').toggle();
			setTimeout('dijit.byId(\'systemMessages\').toggle();', 4000);
		}else{
			dojo.xhrPost({
			  url: "fetchData.php?action=submit",
			  handleAs: "json-comment-filtered",
			  //form: dojo.byId('contentForm'),
			  content: dijit.byId('contentForm').getValues(),
			  timeout: 5000,
			  handle: function(response, ioArgs){
			  	if(response.update == 'true'){
			  		dijit.byId('systemMessages').setContent("You have successfully updated \""+response.title+".\" ");
			  		//clearContentForm();
			  	}else{
			  		dijit.byId('systemMessages').setContent("You have successfully added \""+response.title+".\" ");
			  		//clearContentForm();
			  	}
			  	dijit.byId('systemMessages').toggle();	  	
			  	setTimeout('dijit.byId(\'systemMessages\').toggle();dijit.byId(\'menuContentPane\').refresh();', 4000);
			  }
			});
		
		}
}

//#######################################
//ADD NEW CATEGORY DB
//#######################################
function submitAddNewCategoryForm(){
	dojo.xhrPost({
	  url: "fetchData.php?action=newCat",
	  handleAs: "json-comment-filtered",
	  form: dojo.byId('newCategoryDialog'),
	  timeout: 5000,
	  handle: function(response, ioArgs){
	  	dijit.byId('newCategoryText').setDisplayedValue('');
	  	dijit.byId('newCategoryDialog').hide();
	  	dijit.byId('systemMessages').setContent("You have successfully added the new category: \""+response.category+".\" ");
	  	dijit.byId('systemMessages').toggle();	  	
	  	setTimeout('dijit.byId(\'systemMessages\').toggle();dijit.byId(\'dropDownContentPane\').refresh();dijit.byId(\'menuContentPane\').refresh();', 4000);

	  }
	});


}








//#######################################
//used to process DB SELECT queries DB
//#######################################
function module_select_handler(response,messages,body){

	if(messages){//process error messages if any
		alert(messages);
	}else if(body == 'false'){//process results
		eval('json = ' + response);
		dijit.byId('title').setDisplayedValue(json.data[0].title);
		dijit.byId('category').setValue(json.data[0].category);
		dijit.byId('editID').setDisplayedValue(json.data[0].storyid);
	 }else if(body == 'true'){
	 	var oEditor = FCKeditorAPI.GetInstance('body') ;
		oEditor.SetHTML(response);
	 }
}

//########################################
//set focus to title field after 
//fckeditor has loaded
//########################################
function FCKeditor_OnComplete( editorInstance ){
	if(editId != "null"){
		select_content(editId,XOOPS_URL);
	}
	dojo.byId('title').focus();
}

